//
//  AddRoomVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/7.
//  Copyright © 2020 tym. All rights reserved.
//

#import "RoomEditVC.h"
#import "AddRoomCell.h"
#import "TYMEmptyView.h"
#import "IQKeyboardManager.h"
#import "ShowAlertView.h"
#import "UIView+ZFFrame.h"
#import "UIButton+Gradient.h"
#import "Sizes.h"
#import "RoomModel.h"
#import "LYEmptyViewHeader.h"
#import "FamilyRoomService.h"
#import "RoomEditCell.h"
#import "EquipmentService.h"
#import "GroupShadowTableView.h"
#import "EquipmentTypeModel.h"

@interface RoomEditVC ()<GroupShadowTableViewDelegate,GroupShadowTableViewDataSource,UIScrollViewDelegate>

@property(nonatomic,weak) IBOutlet UIView  *contView;
@property(nonatomic,weak) IBOutlet UITextField *nameFid;
@property(nonatomic,weak) IBOutlet UIButton *saveBtn;

@property(nonatomic,strong)  GroupShadowTableView *tableView;

@property(nonatomic,strong) RoomModel *roomModel;
@property(nonatomic,strong) NSMutableArray *equmentsArray;

@property(nonatomic,strong) FamilyRoomService *roomService;
@property(nonatomic,strong) EquipmentService *equipmentService;
@end

@implementation RoomEditVC

-(instancetype)initWithRoom:(RoomModel*)room WithEquments:(NSMutableArray*)equmentArray{
    self = [super init];
    if (self) {
        self.equmentsArray = equmentArray;
        self.roomModel = room;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    [self addTitle:@"房间编辑"];
    [self setTable];
    self.tz_interactivePopDisabled = YES;
    self.saveBtn.layer.cornerRadius = 5;
    self.saveBtn.layer.masksToBounds = YES;
    self.saveBtn.layer.borderColor = rgba(151, 151, 151).CGColor;
    self.saveBtn.layer.borderWidth = 1;
    _nameFid.text = _roomModel.roomName;
    
    if (self.equmentsArray.count == 0) {
        self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"img_empt_equipment"
                                                                 titleStr:@"该房间暂无可编辑设备"
                                                                detailStr:@""];
        
        [self.tableView reloadData];
        [self.tableView ly_showEmptyView];
    }else{
        self.tableView.isShowArray = @[@(1)];
        self.tableView.headTitles = @[@"设备管理"];
    }
}
 
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}

-(void)backViewFunc:(id)sender{
    if (![_roomModel.roomName isEqualToString:_nameFid.text]) {
        [self editRoomNameAction];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(FamilyRoomService*)roomService{
    if (_roomService == nil) {
        _roomService = [[FamilyRoomService alloc] init];
    }
    return _roomService;
}

-(EquipmentService*)equipmentService{
    if (_equipmentService == nil) {
        _equipmentService = [[EquipmentService alloc] init];
    }
    return _equipmentService;
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, _contView.zf_bottom-NavigateonHeight+20, ScreenWidth, ScreenHeight-NavigateonHeight-_contView.zf_bottom-TabbarHeight-60) style:UITableViewStyleGrouped];
    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    
    if (self.equmentsArray.count) {
        self.tableView.headTitles = @[@"设备管理"];
    }
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

-(IBAction)deleteRoomAction:(id)sender{
    TYMWeak(self);
    [ShowAlertView showAlertViewTitle:@"提示" message:@"确定删除该房间" actionBtnTitle:@"确定"
                           hiddenCancel:NO
                           completion:^(NSInteger actionType) {
        if (actionType) {
            [weakself deleteRoomAction];
        }
    }];
}

-(void)deleteRoomAction{
    TYMWeak(self);
    [self.roomService getDeletefamilyroomFamilyId:self.roomModel.id completion:^(LMJBaseResponse * _Nonnull response) {
        if (response.code == 0) {
            [WHToast showMessage:@"删除成功" duration:2 finishHandler:^{
                [weakself.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }
    }];
}

//编辑房间名称
-(void)editRoomNameAction{
    TYMWeak(self);
    [self.roomService getFamilyroomcreatefamilyroomFamilyId:[UserTool getCurrentFamily].familyId roomName:_nameFid.text  roomDesc:_roomModel.roomDesc image:_roomModel.image  rooID:_roomModel.familyRoomId?_roomModel.familyRoomId:_roomModel.id completion:^(LMJBaseResponse * _Nonnull response) {
        [weakself.navigationController popViewControllerAnimated:YES];
    }];
    
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.equmentsArray.count;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [RoomEditCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RoomEditCell *cell = [tableView dequeueReusableCellWithIdentifier:[AddRoomCell identifier]];
    if (!cell) {
        cell = [[RoomEditCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[RoomEditCell identifier]];
    }
    [cell setEqumentModel:self.equmentsArray[indexPath.row]];
    TYMWeak(self);
    cell.deleteEqumentBlock = ^{
        [weakself deleteEqumentWithIndex:indexPath.row];
    };
    return cell;
}

-(void)deleteEqumentWithIndex:(NSInteger)index{
    if (self.equmentsArray.count == 0) {
        return;
    }
    TYMWeak(self);
    [ShowAlertView showAlertViewTitle:@"提示" message:@"确定移动该设备至公共房间?" actionBtnTitle:@"确定" hiddenCancel:NO completion:^(NSInteger actionType) {
        if (actionType) {
            EquipmentModel *model = self.equmentsArray[index];
            [weakself.equipmentService getMoveEquipment:@[@(model.equipmentId)] familyRoomId:self.pubRoomID completion:^(LMJBaseResponse * _Nonnull response) {
                if (response.message) {
                    [WHToast showMessage:response.message duration:2 finishHandler:nil];
                }else{
                    [WHToast showMessage:@"操作成功" duration:2 finishHandler:nil];
                    [weakself removerTableViewRow:index];
                }
            }];
        }
    }];
}

-(void)removerTableViewRow:(NSInteger)index{
    
    [self.equmentsArray removeObjectAtIndex:index];
    
    if (self.equmentsArray.count == 0) {
        self.tableView.isShowArray = @[@(0)];
        self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"img_empt_equipment"
                                                                 titleStr:@"该房间暂无可编辑设备"
                                                                detailStr:@""];
        [self.tableView reloadData];
        [self.tableView ly_showEmptyView];
    }else{
       [self.tableView reloadData];
    }
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.nameFid resignFirstResponder];
}

@end
